import { app } from "../../script/app/App";
import { bind } from "../../core/module/proxy/ProxyDecorator";
import { AccountProxy } from "../../script/proxy/AccountProxy";
import { RegisterProxy } from "../../script/proxy/RegisterProxy";
import { UIBase } from "../../core/module/ui/UIBase";
import { SceneEnum } from "../../script/share/Enum";
import { Prefab } from "cc";

@bind(AccountProxy)
export class Account extends UIBase<AccountProxy> {
    onInit(): void {
        this._view.btnClose.onClick(this, this._onCloseClick)
        this._view.btnRegister.onClick(this, this._onRegisterClick)
        this._view.btnLogin.onClick(this, this._onAccountLoginClick)
    }

    onShow(): void {
        super.onShow()
        this._view.inputAccount.string = app.model.global.account
        this._view.inputPassword.string = app.model.global.password
        this.loadRes('prefab/register', Prefab)
    }

    private _onAccountLoginClick() {
        const account = this._view.inputAccount.string
        if (!account) {
            app.toast.showTips('请输入账号')
            return
        }
        const password = this._view.inputPassword.string
        if (!password) {
            app.toast.showTips('请输入密码')
            return
        }
        app.model.login.accountLoginReq(account, password, (isInGame: boolean) => {
            if (isInGame) {
                app.model.game.enterRoomReq(app.model.user.userId, () => {
                    app.scene.startScene(SceneEnum.Game)
                })
                return
            }
            app.scene.startScene(SceneEnum.Lobby)
        })
    }

    private _onRegisterClick() {
        app.ui.show(RegisterProxy)
    }

    private _onCloseClick() {
        app.ui.close(AccountProxy)
    }
}


